<?php

namespace app\admin\controller;

class SettlementBase
{
    public function index()
    {
        $query = \app\model\Settlement::field(['id', 'sales_id', 'invoice_url', 'status_value', 'invoice_status_value', 'trade_month', 'electricity_total', 'created_at'])->with(['salesman'])->order('id', 'desc');

        if (request()->has('tradeMonth')) {
            $query->whereLike('trade_month', '%' . input('tradeMonth') . '%');
        }

        if (request()->has('statusValue')) {
            $query->where('status_value', input('statusValue'));
        }

        if (request()->has('invoiceStatusValue')) {
            $query->where('invoice_status_value', input('invoiceStatusValue'));
        }

        $models = $query->select();

        return json(['code' => 20000, 'data' => ['settlements' => $models]]);
    }

    public function delete()
    {
        $model = \app\model\Settlement::destroy(input('id'));

        return json(['code' => 20000, 'msg' => '移除成功', 'data' => $model]);
    }

    public function update()
    {
        $model = \app\model\Settlement::update(input());

        return json(['code' => 20000, 'msg' => '更新成功', 'data' => $model]);
    }

    public function create()
    {
        $param = request()->param();

        $model = \app\model\Settlement::create($param);

        if (!$model) {
            return json(['code' => 40400, 'msg' => '新增失败']);
        }

        return json(['code' => 20000, 'msg' => '新增成功', 'data' => $model]);
    }

    public function detail()
    {
        $query = \app\model\Settlement::field(['id', 'amount', 'sales_id', 'trade_month', 'electricity_total', 'invoice_url']);

        if (request()->has('id')) {
            $query->where('id', input('id'));
        }

        $model = $query->find();

        return json(['code' => 20000, 'data' => ['settlement' => $model]]);
    }
}
